<?php

/*
 * Method for validating form inputs from the module.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise redirects to the module form.
 */
function valid_city($action)
{
    $ci = &get_instance();

    $ci->form_validation
            ->set_rules('code', 'code', 'trim|required|max_length[20]|xss_clean')
            ->set_rules('name', 'name', 'trim|required|max_length[20]|xss_clean')
            ->set_rules('description', 'description', 'trim|required|max_length[1000]|xss_clean');
//          ->set_rules('status', 'status', 'trim|required|max_length[10]|xss_clean');
    
    if($action == 'edit')
    {
        $ci->form_validation->set_rules('id', 'ID', 'trim|required|numeric|max_length[10]|xss_clean');
    }
    
    if ($ci->form_validation->run() == FALSE)
    {
        if($action == 'add')
        {
            view_module_form('Add Module', 'add_failed');
        } else
        {
            $module = $ci->module_model->find( set_value('id') );
            view_module_form('Edit Module', 'edit_failed', $module);
        }
    } else
    {
        return TRUE;
    }
}

/**
 * Formats an array of records to valid response.
 * @access public
 * @param array $cities Array of cities to be rendered
 */
function response_cities($cities)
{

    $response_cities = array();
    foreach($cities as $city)
    {
        $response_city                       = array();
        $response_city['id']                 = clean( $city['id'] );
        $response_city['name']               = clean( $city['name'] );
        $response_city['country_code']       = clean( $city['country_code'] );
        $response_city['district']           = clean( $city['district'] );
        $response_city['population']         = clean( $city['population'] ); 
        
        array_push($response_cities, $response_city);
    } 
    
    // we format our response
    if( count($response_cities) > 0 ) // check if not empty
    {
        format_response('success', 200, 'Response Cities.', NULL, $response_cities);
    }
    else
    {
        format_response('success', 200, 'No Data Available', NULL);
    }
}

/* End of file city_helper.php */
/* Location: ./application/modules/backend-cities/helpers/city_helper.php */